<?php $this->_extends('../_layouts/default_layout'); ?>
<?php $this->_block('contents'); ?>
<link type="text/css" href="<?=Q::ini('custom_system/base_url')?>css/jq.mselect.css" rel="stylesheet" />
<link type="text/css" href="<?=Q::ini('custom_system/base_url')?>css/jq.autocomplete.css" rel="stylesheet" />
<script type="text/javascript" src="<?=Q::ini('custom_system/base_url')?>js/jq.autocomplete.js"></script>
<script type="text/javascript" src="<?=Q::ini('custom_system/base_url')?>js/jq.multiselect.js"></script>
<script type="text/javascript">
get_check = function(){
	var ids = '';
	$(".list_table").find("input[type='checkbox']").each(function(){
		if ($(this).is(":checked"))
		{
			if (ids)
			{
				ids += ',';
			}
			ids += $(this).val();
		}
	});
	return ids;
};
$(function(){
	$("#multiselect").multiselect({height: 260, minWidth: 160});
	$(".ajax_provider").autocomplete("<?=url('Finance::Business/Provider')?>").result(function(event, data, formatted){});
	$(".datepicker").datepicker({dateFormat:'yy-mm-dd',changeMonth:true});
	$(".detail").colorbox({width:"90%", height:"70%", iframe:true});
	$("#search_object_sel").click(function(){
		if ($(this).is(":checked"))
		{
			$("#search_object").show();
		}
		else
		{
			$("#search_object").hide();
		}
	});
	$("#print_btn").click(function(){
		var ids = get_check();
		if( ! ids)
		{
			alert('请选择采购单！')
			return false;
		}
		window.open("<?=url('Purchase::Order/Print')?>&ids=" + ids);
	});
	$("#audit_btn").click(function(){
		var ids = get_check();
		if (! ids)
		{
			alert("请选择采购单！");
			return false;
		}
		window.location.href = "<?=url('Finance::Business/PurchaseOrderAudit')?>&ids=" + ids;
	});
	$("#check_all").click(function(){
		if ($(this).is(":checked"))
		{
			$(".list_table input[type='checkbox']").attr("checked", true);
		}
		else
		{
			$(".list_table input[type='checkbox']").attr("checked", false);
		}
	});
	$("#request_download").click(function(){
		if ( ! confirm('确定下载？'))
		{
			return false;
		}
		var current_tag = $(this);
		var url_arr_json = current_tag.prev().val();
		window.location.href="<?=url('Finance::Business/ExportPurchaseOrderInfo')?>&url_arr_json=" + url_arr_json;
	});
});
</script>
<fieldset><legend>查询条件</legend>
<form action="" method="get">
<? include(Q::ini('custom_system/elements_dir') . 'searchform_element.php');?>
<table class="form_table">
<tr>
	<th>采购日期</th>
	<td>
		<input type="text" name="begin_purchase_time" class="datepicker" value="<?=isset($_GET['begin_purchase_time'])?$_GET['begin_purchase_time']:''?>" /> to
		<input type="text" name="end_purchase_time" class="datepicker" value="<?=isset($_GET['end_purchase_time'])?$_GET['end_purchase_time']:''?>" />
	</td>
</tr>
<tr>
	<th>
		<label><input type="checkbox" id="search_object_sel" name="search_object_sel" value="1" <?=isset($_GET['search_object_sel']) && $_GET['search_object_sel'] == 1 ? 'checked="checked"' : ''?> />入库日期</label>
	</th>
	<td>
		<input type="text" name="begin_storage_audit_time" class="datepicker" value="<?=isset($_GET['begin_storage_audit_time'])?$_GET['begin_storage_audit_time']:''?>" /> to
		<input type="text" name="end_storage_audit_time" class="datepicker" value="<?=isset($_GET['end_storage_audit_time'])?$_GET['end_storage_audit_time']:''?>" />
	</td>
</tr>
<tr <?=isset($_GET['search_object_sel']) && $_GET['search_object_sel'] == 1 ? '' : 'style="display: none"'?> id="search_object">
	<th>显示对象</th>
	<td>
		<label><input type="radio" name="search" value="1" checked="checked" /> 采购单入库</label>
		<label><input type="radio" name="search" value="2" <?=isset($_GET['search']) && $_GET['search'] == 2 ? 'checked=checked' : ''?>/> 产品入库</label>
		<label><input type="radio" name="search" value="3" <?=isset($_GET['search']) && $_GET['search'] == 3 ? 'checked=checked' : ''?>/> 跨越入库</label>
	</td>
</tr>
<tr>
	<th>财务审核日期</th>
	<td>
		<input type="text" name="begin_finance_audit_time" class="datepicker" value="<?=isset($_GET['begin_finance_audit_time'])?$_GET['begin_finance_audit_time']:''?>" /> to
		<input type="text" name="end_finance_audit_time" class="datepicker" value="<?=isset($_GET['end_finance_audit_time'])?$_GET['end_finance_audit_time']:''?>" />
	</td>
</tr>
<tr>
	<th>跨天入库日期</th>
	<td>
		<input type="text" name="begin_cross_days_time" class="datepicker" value="<?=isset($_GET['begin_cross_days_time'])?$_GET['begin_cross_days_time']:''?>" /> to
		<input type="text" name="end_cross_days_time" class="datepicker" value="<?=isset($_GET['end_cross_days_time'])?$_GET['end_cross_days_time']:''?>" />
	</td>
</tr>
<tr>
	<th>采购单号</th>
	<td><input type="text" name="purchase_order_number" value="<?=isset($_GET['purchase_order_number']) ? $_GET['purchase_order_number'] : ''?>" /></td>
</tr>
<tr>
	<th>供应商</th>
	<td><input type="text" name="ajax_provider" class="ajax_provider" value="<?=isset($_GET['ajax_provider']) ? $_GET['ajax_provider'] : ''?>" /></td>
</tr>
<tr>
	<th>结款类型</th>
	<td>
		<select name="purchase_order_payment_type">
			<option value="">请选择..</option>
			<? foreach (Q::ini('custom_flag/purchase_order_payment_type') as $pt):?>
			<option value="<?=$pt['value']?>" <?if (isset($_GET['purchase_order_payment_type']) && $_GET['purchase_order_payment_type'] == $pt['value'])echo "selected='selected'" ?>><?=$pt['name']?></option>
			<? endforeach;?>
		</select>
	</td>
</tr>
<tr>
	<th>采购人员</th>
	<td>
		<select name="purchase_user_id">
			<option value="">请选择..</option>
			<?foreach ($user as $u):?>
			<option value="<?=$u['id']?>" <?if (isset($_GET['purchase_user_id'])&&$_GET['purchase_user_id']==$u['id'])echo "selected='selected'"?>><?=$u['name']?></option>
			<?endforeach;?>
		</select>
	</td>
</tr>
	<tr>
		<th>仓库</th>
		<td>
			<select name="warehouse_id[]" multiple="multiple" id="multiselect">
			<?foreach ($warehouse_group as $k => $sg):?>
			<optgroup label="<?=$k?>">
			<?foreach ($sg as $s):?>
			<option value="<?=$s['id']?>" <?if (isset($_GET['warehouse_id']) && is_array($_GET['warehouse_id']) && in_array($s['id'], $_GET['warehouse_id']))echo "selected='selected'";elseif (isset($_GET['warehouse_id']) && $_GET['warehouse_id'] == $s['id'])echo "selected='selected'"?>><?=$s['name']?></option>
			<?endforeach;?>
			</optgroup>
			<?endforeach;?>
			</select>
		</td>
		</tr>
<tr>
	<th>状态</th>
	<td>
		<?foreach (Q::ini('custom_flag/purchase_order_status') as $ps):?>
		<label><input type="checkbox" name="purchase_order_status[<?=$ps['value']?>]" value="<?=$ps['value']?>" <?if (isset($_GET['purchase_order_status']) && array_key_exists($ps['value'], $_GET['purchase_order_status'])) echo "checked='checked'"?> /> <?=$ps['name']?></label>
		<?endforeach;?>
	</td>
</tr>
<tr>
	<th>财务审核</th>
	<td>
		<?foreach (Q::ini('custom_flag/purchase_order_finance_audit') as $fa):?>
		<label><input type="checkbox" name="finance_audit[<?=$fa['value']?>]" value="<?=$fa['value']?>" <?if (isset($_GET['finance_audit']) && array_key_exists($fa['value'], $_GET['finance_audit'])) echo "checked='checked'";?> /> <?=$fa['name']?></label>
		<?endforeach;?>
	</td>
</tr>
<tr>
	<td><input type="hidden" name="is_search" value="1" /></td>
	<td><input type="submit" class="button" value="查询" /></td>
	<td>
		<?if (isset($url_arr_json)):?>
		<input type="hidden" value='<?=$url_arr_json?>' />
		<input type="button" class="button" value="请求下载" id="request_download" />
		<?endif;?>
	</td>
</tr>
</table>
</form>
</fieldset>

<table class="list_table" width="100%" >
	<tr>
		<th width="40"></th>
		<th width="120">采购单号</th>
		<th width="100">所在仓库</th>
		<th width="120">采购日期</th>
		<th width="120">入库日期</th>
		<th width="120">财务审核日期</th>
		<th width="120">采购人</th>
		<th width="100">预计总价</th>
		<th width="100">实际总价</th>
		<th width="100">状态</th>
		<th width="120">结款类型</th>
		<th>操作</th>
	</tr>
	<?foreach ($purchase_order as $po):?>
	<tr>
		<td><input type="checkbox" value="<?=$po['id']?>" /></td>
		<td><a class="detail" href="<?=url('Finance::Business/PurchaseDetail', array('id'=>$po['id']))?>"><?=$po['number']?></a></td>
		<td><?=$po['warehouse_name']?></td>
		<td><?=date('Y-m-d', strtotime($po['purchase_time']))?></td>
		<td><?=isset($po['storage_audit_time']) ? date('Y-m-d', strtotime($po['storage_audit_time'])) : ''?></td>
		<td><?=isset($po['finance_audit_time']) ? date('Y-m-d', strtotime($po['finance_audit_time'])) : ''?></td>
		<td><?=$po['purchase_user_name']?></td>
		<td><?=sprintf('%.2f', $po['budget_amount'])?></td>
		<td><?=sprintf('%.2f', $po['actual_amount'])?></td>
		<td><?=Helper_BSS_Normal::getIniOne('purchase_order_status', 'value', $po['status'], 'name')?></td>
		<td><?=Helper_BSS_Normal::getIniOne('purchase_order_payment_type', 'value', $po['payment_type'], 'name')?></td>
		<td>
			<?if ($po['finance_audit'] == Q::ini('custom_flag/purchase_order_finance_audit/unaudit/value') && ($po['status'] == Q::ini('custom_flag/purchase_order_status/storaged/value') || $po['status'] == Q::ini('custom_flag/purchase_order_status/send/value'))):?>
			<a href="<?=url('Finance::Business/PurchaseOrderAudit', array('ids' => $po['id']))?>" onclick="if(!confirm('确定审核？')) return false;">审核</a> |  
			<a href="<?=url('Finance::Business/UpdatePurchaseOrderActualAmount', array('ids' => $po['id']))?>" onclick="if(!confirm('确定重新计算，更新采购单总金额？')) return false;">重新计算总金额</a>
			<?else:?>
			<font color="#aaaaaa">审核</font>
			<?endif;?>
		</td>
	</tr>
	<?endforeach;?>
</table>
<?=$page?>
<table class="list_table">
<tr>
	<td><label><input type="checkbox" id="check_all" /> 全选</label></td>
	<td>
		<input type="button" class="button" id="audit_btn" value="合并审核" /> |
		<input type="button" class="button" value="合并打印" id="print_btn" />
	</td>
</tr>
</table>
<?if (isset($_GET['search_object_sel']) && ! empty($_GET['begin_storage_audit_time']) && ! empty($_GET['end_storage_audit_time'])):?>
入库日期内采购单的总金额：RMB <strong><?=number_format($amount, 2)?></strong><br />
入库日期内采购产品总金额：RMB <strong><?=number_format($item_amount, 2)?></strong>
<?else:?>
搜索条件的总金额：RMB <strong><?=number_format($amount, 2)?></strong>
<?endif;?>
<?php $this->_endblock();?>